package cate.game.play.config.destiny;

import cate.common.table.fight.destiny.DestinyRow;
import cate.game.play.support.PlayBuildContext;
import cate.game.table.TableManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DestinyCfgHolder {

	private Map<Integer, DestinyCfg> map = new HashMap<>();

	private List<DestinyCfg> list = new ArrayList<>();

	public DestinyCfg get(int id) {
		return map.get(id);
	}

	public List<DestinyCfg> getList(){
		return list;
	}

	public void init(TableManager table) {
		map.clear();
		list.clear();
		table.destiny.base.getList().forEach(this::create);
	}

	public void init(PlayBuildContext context) {
		map.clear();
		list.clear();
		context.destinyExcel().base.getList().forEach(this::create);
	}

	private void create(DestinyRow row){
		DestinyCfg cfg = new DestinyCfg().init(row);
		map.put(cfg.id, cfg);
		list.add(cfg);
	}

	public DestinyCfg find(int differ) {
		for (DestinyCfg cfg : list) {
			if (differ >= cfg.row.start && differ <= cfg.row.end) {
				return cfg;
			}
		}
		return null;
	}
}
